package com.imti.mall.product.mapper;

import com.imti.mall.model.pms.pojo.PmsProduct;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class ProductDynaSqlProvider {
    /**
     * 根据条件查询商品信息
     * @param
     * @return
     */
    public String findPmsProduct(Map<String,Object> map){

        return new SQL(){
            {
                SELECT(" id,brand_id,product_category_id,feight_template_id,product_attribute_category_id,`name`,pic,product_sn,delete_status,publish_status,new_status,recommand_status,verify_status,sort,sale,price,promotion_price,gift_growth,gift_point,use_point_limit,sub_title,description,original_price,stock,low_stock,unit,weight,preview_status,service_ids,keywords,note,album_pics,detail_title,detail_desc,detail_html,detail_mobile_html,promotion_start_time,promotion_end_time,promotion_per_limit,promotion_type,brand_name,product_category_name ");
                FROM(" pms_product ");
                StringBuffer sb=new StringBuffer();
                PmsProduct pmsProduct= (PmsProduct) map.get("pmsProduct");
                if(pmsProduct!=null) {

                    //按照货号进行查询
                    if (pmsProduct.getProductSn() != null) {
                        sb.append(" and  product_sn=#{pmsProduct.productSn}");
                    }
                    //根据商品分类进行查询
                    if (pmsProduct.getProductCategoryName() != null) {
                        sb.append(" and  product_category_name=#{pmsProduct.productCategoryName}");
                    }

                    //根据商品id分类进行查询
                    if (pmsProduct.getProductCategoryId() != null) {
                        sb.append(" and  product_category_id=#{pmsProduct.productCategoryId}");
                    }

                    //根据商品品牌进行查询
                    if (pmsProduct.getBrandName() != null) {
                        sb.append(" and  brand_name=#{pmsProduct.brandName}");
                    }

                    //根据商品品牌进行查询
                    if (pmsProduct.getBrandId() != null) {
                        sb.append(" and  brand_id=#{pmsProduct.brandId}");
                    }

                    //根据商品上架进行查询
                    if (pmsProduct.getPublishStatus() != null) {
                        sb.append(" and publish_status=#{pmsProduct.publishStatus}");
                    }

                    //根据审核状态进行查询
                    if (pmsProduct.getVerifyStatus() != null) {
                        sb.append(" and verify_status=#{pmsProduct.verifyStatus}");
                    }
                    //按照商品名称进行查询
                    if (pmsProduct.getName()!= null) {
                        sb.append(" and name  LIKE concat('%',#{pmsProduct.name},'%')");
                    }
                }
                WHERE(" delete_status=0 "+sb.toString());
                ORDER_BY(" sort DESC");
                LIMIT(" #{pageNum},#{pageSize} ");
            }
        }.toString();

    }


    /**
     * 总条数查询商品信息
     * @param pmsProduct
     * @return
     */
    public String countPmsProduct(final PmsProduct pmsProduct){

        return new SQL(){
            {
                SELECT(" COUNT(*)");
                FROM(" pms_product ");
                StringBuffer sb=new StringBuffer();
                if(pmsProduct!=null) {

                    //按照货号进行查询
                    if (pmsProduct.getProductSn() != null) {
                        sb.append(" and  product_sn=#{productSn}");
                    }
                    //根据商品分类进行查询
                    if (pmsProduct.getProductCategoryName() != null) {
                        sb.append(" and  product_category_name=#{productCategoryName}");
                    }

                    //根据商品id分类进行查询
                    if (pmsProduct.getProductCategoryId() != null) {
                        sb.append(" and  product_category_id=#{productCategoryId}");
                    }

                    //根据商品品牌进行查询
                    if (pmsProduct.getBrandName() != null) {
                        sb.append(" and  brand_name=#{brandName}");
                    }

                    //根据商品品牌进行查询
                    if (pmsProduct.getBrandId() != null) {
                        sb.append(" and  brand_id=#{brandId}");
                    }

                    //根据商品上架进行查询
                    if (pmsProduct.getPublishStatus() != null) {
                        sb.append(" and publish_status=#{publishStatus}");
                    }

                    //根据审核状态进行查询
                    if (pmsProduct.getVerifyStatus() != null) {
                        sb.append(" and verify_status=#{verifyStatus}");
                    }
                    //按照商品名称进行查询
                    if (pmsProduct.getName()!= null) {
                        sb.append(" and name  LIKE concat('%',#{name},'%')");
                    }
                }
                WHERE(" delete_status=0 "+sb.toString());
            }
        }.toString();

    }



    /**
     * 根据id修改单个商品信息详情
     * @param
     * @return
     */
    public String updategetPmsProductById(final PmsProduct pmsProductp){

        return new SQL(){
            {
                UPDATE(" pms_product ");
                if(pmsProductp!=null) {


                    if (pmsProductp.getBrandId() != null) {
                       SET("   brand_id=#{brandId}");
                    }

                    if (pmsProductp.getProductCategoryId() != null) {
                        SET("   product_category_id=#{productCategoryId}");
                    }

                    if (pmsProductp.getFeightTemplateId() != null) {
                        SET("   feight_template_id=#{feightTemplateId}");
                    }

                    if (pmsProductp.getProductAttributeCategoryId() != null) {
                        SET("   product_attribute_category_id=#{productAttributeCategoryId}");
                    }

                    if (pmsProductp.getName() != null) {
                        SET("   name=#{name}");
                    }

                    if (pmsProductp.getPic() != null) {
                        SET("   pic=#{pic}");
                    }

                    if (pmsProductp.getProductSn() != null) {
                        SET("   product_sn=#{productSn}");
                    }

                    if (pmsProductp.getDeleteStatus() != null) {
                        SET("   delete_status=#{deleteStatus}");
                    }

                    if (pmsProductp.getPublishStatus() != null) {
                        SET("   publish_status=#{publishStatus}");
                    }

                    if (pmsProductp.getNewStatus() != null) {
                        SET("   new_status=#{newStatus}");
                    }

                    if (pmsProductp.getRecommandStatus() != null) {
                        SET("   recommand_status=#{recommandStatus}");
                    }

                    if (pmsProductp.getVerifyStatus() != null) {
                        SET("   verify_status=#{verifyStatus}");
                    }

                    if (pmsProductp.getSort() != null) {
                        SET("   sort=#{sort}");
                    }


                    if (pmsProductp.getSale() != null) {
                        SET("   sale=#{sale}");
                    }
                    if (pmsProductp.getPrice() != null) {
                        SET("   price=#{price}");
                    }
                    if (pmsProductp.getPromotionPrice() != null) {
                        SET("   promotion_price=#{promotionPrice}");
                    }
                    if (pmsProductp.getGiftGrowth() != null) {
                        SET("   gift_growth=#{giftGrowth}");
                    }
                    if (pmsProductp.getGiftPoint() != null) {
                        SET("   gift_point=#{giftPoint}");
                    }
                    if (pmsProductp.getUsePointLimit() != null) {
                        SET("   use_point_limit=#{usePointLimit}");
                    }

                    if (pmsProductp.getSubTitle() != null) {
                        SET("   sub_title=#{subTitle}");
                    }
                    if (pmsProductp.getDescription() != null) {
                        SET("   description=#{description}");
                    }

                    if (pmsProductp.getOriginalPrice() != null) {
                        SET("   original_price=#{originalPrice}");
                    }
                    if (pmsProductp.getStock() != null) {
                        SET("   stock=#{stock}");
                    }
                    if (pmsProductp.getLowStock() != null) {
                        SET("   low_stock=#{lowStock}");
                    }
                    if (pmsProductp.getUnit() != null) {
                        SET("   unit=#{unit}");
                    }

                    if (pmsProductp.getWeight() != null) {
                        SET("   weight=#{weight}");
                    }
                    if (pmsProductp.getPreviewStatus() != null) {
                        SET("   preview_status=#{previewStatus}");
                    }
                    if (pmsProductp.getServiceIds() != null) {
                        SET("   service_ids=#{serviceIds}");
                    }
                    if (pmsProductp.getKeywords() != null) {
                        SET("   keywords=#{keywords}");
                    }
                    if (pmsProductp.getNote() != null) {
                        SET("   note=#{note}");
                    }
                    if (pmsProductp.getAlbumPics() != null) {
                        SET("   album_pics=#{albumPics}");
                    }
                    if (pmsProductp.getDetailTitle() != null) {
                        SET("   detail_title=#{detailTitle}");
                    }

                    if (pmsProductp.getDetailDesc() != null) {
                        SET("  detail_desc=#{detailDesc}");
                    }
                    if (pmsProductp.getDetailHtml() != null) {
                        SET("   detail_html=#{detailHtml}");
                    }
                    if (pmsProductp.getDetailMobileHtml() != null) {
                        SET("   detail_mobile_html=#{detailMobileHtml}");
                    }
                    if (pmsProductp.getPromotionStartTime() != null) {
                        SET("   promotion_start_time=#{promotionStartTime}");
                    }
                    if (pmsProductp.getPromotionEndTime() != null) {
                        SET("   promotion_end_time=#{promotionEndTime}");
                    }

                    if (pmsProductp.getPromotionPerLimit() != null) {
                        SET("   promotion_per_limit=#{promotionPerLimit}");
                    }

                    if (pmsProductp.getPromotionType() != null) {
                        SET("   promotion_type=#{promotionType}");
                    }
                    if (pmsProductp.getBrandName() != null) {
                        SET("   brand_name=#{brandName}");
                    }
                    if (pmsProductp.getProductCategoryName() != null) {
                        SET("   product_category_name=#{productCategoryName}");
                    }
                }
                WHERE(" id=#{id} ");

            }
        }.toString();

    }

}
